//
//  DeliveryHV.m
//  ProjectforHDX
//
//  Created by HDX on 16/8/8.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "DeliveryHV.h"

#define DEL_LABEL_HEIGHT 32
#define DEL_LINE_COLOR  [UtilCheck hexStringToColor:@"e1e1df" alpha:1]
#define DEL_FontSize 14
@interface DeliveryHV()



@property(nonatomic,copy) DeliveryHVBlock clickBlock;

@end


@implementation DeliveryHV

/**
 *  创建头部图层
 *
 *  @param frame
 *  @param orderInfo 订单信息
 *  @param rec       物流信息
 *  @param success   点击回调
 *
 *  @return
 */
-(id)initDHVWithFram:(CGRect)frame OrderInfo:(NSDictionary*)orderInfo onClick:(DeliveryHVBlock)success;
{
    self=[super initWithFrame:frame];
    if(self){
        self.clickBlock=success;
        [self setBackgroundColor:[UIColor whiteColor]];
        //订单号
        RZCustomLable *labOrderCode=[[RZCustomLable alloc] init];
        [labOrderCode setFont:CustomFont(DEL_FontSize)];
        [labOrderCode setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
        [self addSubview:labOrderCode];
        
        
        [labOrderCode setFrame:CGRectMake(10, 0, self.width-20, DEL_LABEL_HEIGHT)];
        labOrderCode.text=[NSString stringWithFormat:@"%@ :%@",[THMangerTool language:@"main.orderCode"],[orderInfo objectForKey:@"orderCode"]];
        labOrderCode.numberOfLines=1;
        [labOrderCode sizeToFit];
        labOrderCode.left=10;
        labOrderCode.height=DEL_LABEL_HEIGHT;
 
        //日期
        RZCustomLable *labDate=[[RZCustomLable alloc] init];
        [labDate setFont:CustomFont(DEL_FontSize)];
        [labDate setTextColor:[UtilCheck hexStringToColor:@"9d9fa1" alpha:1] ];
        [self addSubview:labDate];
        [labDate setFrame:CGRectMake(10, 0, self.width-20, DEL_LABEL_HEIGHT)];
        labDate.text=[orderInfo objectForKey:@"orderCreateTime"];
        labDate.numberOfLines=1;
        [labDate sizeToFit];
        labDate.left=self.width-labDate.width-10;
        labDate.height=labOrderCode.height;
        
        //线
        [self addSoldLine:1 soldcolor:DEL_LINE_COLOR pointA:CGPointMake(0, labOrderCode.bottom) pointB:CGPointMake(self.width, labOrderCode.bottom)];
        
        //收货地址
        UIView *recView=[[UIView alloc] initWithFrame:CGRectMake(0, labDate.bottom, self.width, 0)];
        [self addSubview:recView];
        
        //姓名
        RZCustomLable *labTitle=[[RZCustomLable alloc] init];
        [labTitle setFont:CustomFont(DEL_FontSize)];
        [labTitle setTextColor:[UtilCheck hexStringToColor:@"a1a1a1" alpha:1] ];
        [recView addSubview:labTitle];
        [labTitle setFrame:CGRectMake(10, 5, self.width-60, DEL_LABEL_HEIGHT)];
        labTitle.text=[NSString stringWithFormat:@"%@",[orderInfo objectForKey:@"customer"]];
        labTitle.numberOfLines=1;
        [labTitle sizeToFit];
        labTitle.left=10;
        labTitle.height=DEL_LABEL_HEIGHT;
        
        //希望送达时间
        RZCustomLable *labWishDate=[[RZCustomLable alloc] init];
        [labWishDate setFont:CustomFont(DEL_FontSize)];
        [labWishDate setTextColor:[UtilCheck hexStringToColor:@"a1a1a1" alpha:1] ];
        [recView addSubview:labWishDate];
        [labWishDate setFrame:CGRectMake(labTitle.right+40, 5, self.width-60, DEL_LABEL_HEIGHT)];
        labWishDate.text=[NSString stringWithFormat:@"送达时间:%@",[orderInfo objectForKey:@"arriveDate"]];
        if ([orderInfo objectForKey:@"arriveDate"] == nil || [[orderInfo objectForKey:@"arriveDate"] length] < 1) {
            labWishDate.hidden = YES;
        }
        labWishDate.numberOfLines=1;
        [labWishDate sizeToFit];
        labWishDate.font = [UIFont systemFontOfSize:12];
        labWishDate.height=DEL_LABEL_HEIGHT;

        
        //地址
        RZCustomLable *labAddress=[[RZCustomLable alloc] init];
        [labAddress setFont:CustomFont(DEL_FontSize)];
        [labAddress setTextColor:[UtilCheck hexStringToColor:@"a1a1a1" alpha:1] ];
        [recView addSubview:labAddress];
        [labAddress setFrame:CGRectMake(10, labTitle.bottom-5, recView.width-60, DEL_LABEL_HEIGHT)];
        labAddress.text=[NSString stringWithFormat:@"%@",[orderInfo objectForKey:@"address"]];
        labAddress.numberOfLines=0;
 
        [labAddress sizeToFit];
        labAddress.left=labTitle.left;
        labAddress.height=DEL_LABEL_HEIGHT;
        
            recView.height=labAddress.bottom+5;
        //图标
        RZCustomImageView *rightIcon=[[RZCustomImageView alloc] init];
        UIImage *image=BDFILEIMAGENAEM(@"Order/img_location");
        [rightIcon setImage:image];
        [recView addSubview:rightIcon];
        //右按钮
        [rightIcon setFrame:CGRectMake(10,(recView.height-image.size.height)/2.f, image.size.width, image.size.height)];
        labTitle.left=rightIcon.right+10;
        labAddress.left=labTitle.left;
 
    
        self.height=recView.bottom;
       
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizer:)];
        [self addGestureRecognizer:tap];
        

        CustomLabel *labUrgent=[[CustomLabel alloc] initWithFrame:CGRectMake(recView.right, 0, recView.width, 300)];
        [labUrgent setFont:CustomFont(DEL_FontSize+1)];
        
        NSString *htmlString=[NSString stringWithFormat:@"%@:%@",[THMangerTool language:@"delivery.arriveTime"],[orderInfo objectForKey:@"arriveTime"] ];
        //展示是否加急
        if([[orderInfo allKeys] containsObject:@"isUrgent"]){
            htmlString=[orderInfo objectForKey:@"isUrgent"] ;
        }
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        labUrgent.attributedText=attrStr;
        labUrgent.numberOfLines=4;
        
        CGSize size=[labUrgent  preferredSizeWithMaxWidth:recView.width];
        labUrgent.width=size.width;
        labUrgent.left=recView.width-labUrgent.width-10;
        labUrgent.height=size.height;
        if(labUrgent.height<DEL_LABEL_HEIGHT){
            labUrgent.height=DEL_LABEL_HEIGHT;
        }
        [recView addSubview:labUrgent];
   
     
        
    }
    return self;
    
}

-(void)tapRecognizer:(UITapGestureRecognizer*)sender{
    
    if(self.clickBlock){
        self.clickBlock();
    }
    
}

@end
